var express = require('express');
var router = express.Router();
var mongoose = require("mongoose");
var Goods = require("../models/goods.js");
var Users = require("../models/users.js");

//链接数据库
mongoose.connect('mongodb://127.0.0.1:27017/dumall');
mongoose.connection.on('connected', function () {
  console.log("数据库链接成功");
})
mongoose.connection.on('error', function () {
  console.log("数据库链接失败");
})

//获取商品的信息
router.get('/list', function (req, res, next) {
  let page = parseInt(req.param("page"));
  let pageSize = parseInt(req.param("pageSize"));
  let sort = req.param("sort")
  let priceLevel = req.param("priceLevel");
  let skip = (page - 1) * pageSize;
  var params = {};
  var priceGt = "";
  var priceLt = "";
  if (priceLevel != "all") {
    switch (priceLevel) {
      case '0':
        priceGt = 0;
        priceLt = 100;
        break;
      case '1':
        priceGt = 100;
        priceLt = 1000;
        break;
      case '2':
        priceGt = 1000;
        priceLt = 2000;
        break;
      case '3':
        priceGt = 2000;
        priceLt = 5000;
        break;

    }
    params = {
      salePrice: {
        $gt: priceGt,
        $lte: priceLt
      }
    }
  }
  let goodModel = Goods.find(params).skip(skip).limit(pageSize)
  goodModel.sort({
    'salePrice': sort
  })
  goodModel.exec(function (err, doc) {
    if (err) {
      res.json({
        status: "1",
        msg: err.message
      })
    } else {
      res.json({
        status: "0",
        msg: "",
        result: {
          count: doc.length,
          list: doc
        }
      })
    }
  })
});

//添加到购物车

router.post("/addCart", function (req, res, next) {
  var userId = "100000077";
  var productId = req.body.productId;
  var productNum = "";
  var checked = ""
  Users.findOne({
    userId: userId
  }, function (err, userDoc) {
    if (err) {
      res.json({
        status: "1",
        msg: err.message
      })
    } else {
      // console.log("userDoc:" + userDoc)
      if (userDoc) {
        let goodsItem = "";
        userDoc.cartList.forEach((item) => {
          // console.log(item)
          if (item.productId == productId) {
            goodsItem = item;
            item.productNum++
          }
        });
        if (goodsItem) {
          userDoc.save(function (err2, doc2) {
            if (err2) {
              res.json({
                status: "1",
                msg: err2.message
              })
            } else {
              res.json({
                status: "0",
                msg: "",
                result: "success"
              })
            }
          })
        } else {
          Goods.findOne({
            productId: productId
          }, function (err1, doc) {
            if (err1) {
              res.json({
                status: "1",
                msg: err1.message
              })
            } else {
              if (doc) {
                // console.log(doc)
                doc.productNum = 1;
                doc.checked = 1;
                userDoc.cartList.push(doc);
                userDoc.save(function (err2, doc2) {
                  if (err2) {
                    res.json({
                      status: "1",
                      msg: err2.message
                    })
                  } else {
                    res.json({
                      status: "0",
                      msg: "",
                      result: "success"
                    })
                  }
                })
              }
            }
          })
        }

      }
    }
  })
})


module.exports = router;
